﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using InfoJetSoft.Service.Entity;

//CID 0618 //2011.05.30 //Begin
namespace InfoJetSoft.Service.Util
{
    class ResourceFileUtil
    {
        //CID 0618 //2010.07.05 //Begin
        public static string getResourceFileUrl(Manifest manifest, string fileName)
        {
            if (manifest.JetContext.ResourceFileStorage != null)
            {
                return manifest.JetContext.ResourceFileStorage.GetResourceFileUrl(manifest.Id, fileName);
            }
            else
            {
                string resourceFileFolder = ConfigUtil.ResourceFilePath();
                if (resourceFileFolder == null)
                {
                    return fileName;
                }
                else
                {
                    if (resourceFileFolder.EndsWith("/") || resourceFileFolder.EndsWith("\\"))
                    {
                        resourceFileFolder = resourceFileFolder + manifest.ResourceFolderName;
                    }
                    else
                    {
                        resourceFileFolder = resourceFileFolder + "/" + manifest.ResourceFolderName;
                    }
                    //CID 0103 //2008.08.26 //Begin
                    //如果用OnTemplateFilesReadyEvent设置了ResourceFolderName,一般是重定向到一个带参数的Page, 这时ResourceFolderName一般都包含?.
                    //如果包含了?,就不用再追加"/"了.
                    if (resourceFileFolder.IndexOf("?") < 0)
                    {
                        resourceFileFolder = resourceFileFolder + "/";
                    }
                    //CID 0103 //2008.08.26 //End
                    //CID 0618 //2010.07.05 //Begin
                    return resourceFileFolder + fileName;
                    //CID 0618 //2010.07.05 //End
                }
            }
        }
        //CID 0618 //2010.07.05 //End

        public static void copyResource(HttpContext context, InfoJetContext jetContext, Manifest manifest, CABFile cabFile)
        {
            //CID 0618 //2011.05.29 //Begin
            if (jetContext.ResourceFileStorage != null)
            {
                jetContext.ResourceFileStorage.Cache(manifest.Id, cabFile.Name, cabFile.ByteOutput);
            }
            else
            {
                //将资源文件写入服务器ResourceFilePath目录。
                //CID 0618 //2011.05.29 //End
                //如果没有定义ResourceFilePath则退出。
                string resourceFilePath = ConfigUtil.ResourceFilePath();
                if (resourceFilePath == null)
                {
                    return;
                }

                //如果还没有生成ResourceFolder，为当前表单模板生成一个。
                if (manifest.ResourceFolderName == null)
                {
                    //CID 0618 //2010.06.28 //Begin
                    manifest.ResourceFolderName = manifest.Id;
                    //CID 0618 //2010.06.28 //End
                    //manifest.ResourceFolderPath用于在移除表单模板时,清空表单模板的资源目录.
                    manifest.ResourceFolderPath = context.Request.MapPath(resourceFilePath) + "\\" + manifest.ResourceFolderName;
                    //创建目录.
                    DirectoryInfo dir = Directory.CreateDirectory(manifest.ResourceFolderPath);
                }

                //写文件。
                try
                {
                    FileStream stream = File.Create(manifest.ResourceFolderPath + "\\" + cabFile.Name);
                    stream.Write(cabFile.ByteOutput, 0, cabFile.ByteOutput.Length);
                    stream.Close();
                }
                catch (Exception e)
                {
                    //do nothing.
                }
            }
            //CID 0618 //2011.05.29 //End
        }
    }
}
//CID 0618 //2011.05.30 //End
